package cuongnh.project.japanesediary.vocabulary.ui.editor.kanji;

import java.util.Iterator;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.IFormPage;
import org.springframework.beans.factory.annotation.Autowired;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Kanji;
import cuongnh.project.japanesediary.vocabulary.ui.editor.kanji.page.category.KanjiCategoryMDPage;
import cuongnh.project.japanesediary.vocabulary.ui.editor.kanji.page.compound.CompoundWordsPage;
import cuongnh.project.japanesediary.vocabulary.ui.editor.kanji.page.general.KanjiGeneralInfoPage;

public class KanjiEditor extends FormEditor implements
		IWorkbenchWindowActionDelegate {

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.ui.editor.kanji.KanjiEditor"; //$NON-NLS-1$

	private IWorkbenchWindow window;

	@Autowired
	private CommonServices commonServices;

	private Kanji kanji;

	@Override
	protected void addPages() {
		try {
			addPage(new KanjiGeneralInfoPage(this, "1", "General information",
					kanji, commonServices));
			addPage(new CompoundWordsPage(this, "2", "Compound words",
					kanji, commonServices));
			addPage(new KanjiCategoryMDPage(this, "3", commonServices));
			// addPage(new RelatedGrammarPage(this, grammar,
			// commonServices, "3", "Realted grammars"));
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		for (Iterator<?> iter = this.pages.iterator(); iter.hasNext();) {
			Object obj = iter.next();
			if (obj instanceof IFormPage) {
				((IFormPage) obj).doSave(null);
			}
		}

		// Update dirty state changed
		this.editorDirtyStateChanged();
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void run(IAction action) {
		IWorkbenchPage page = window.getActivePage();
		try {
			page.openEditor(new KanjiEditorInput(1), this.ID);
		} catch (PartInitException e) {
			System.out.println(e);
		}

	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window = window;

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		super.init(site, input);
		if (input instanceof KanjiEditorInput) {
			this.kanji = commonServices.getKanjiDao().get(
					((KanjiEditorInput) input).getId());
			setPartName("K:" + kanji.getKanji());
		}
	}

	@Override
	protected void setPartName(String partName) {
		super.setPartName(partName);
	}

}
